package com.wac.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.Comment;
import com.wac.data.entity.Like;
import com.wac.data.mapper.CommentMapper;
import com.wac.data.mapper.LikeMapper;
import com.wac.data.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wac.data.service.LikeService;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

	@Resource
	private CommentMapper commentMapper;
	@Resource
	private LikeMapper likeMapper;
	@Resource
	private UserService userService;
	@Resource
	private LikeService likeService;

	private Page<Comment> thePage;


	@Override
	public Map<String, Object> selectByUserId(int page, String id) {
		thePage = new Page<>(page, 5);
		commentMapper.selectPage(thePage, new QueryWrapper<Comment>().eq("user_id", id).orderByDesc("time"));
		thePage.getRecords().forEach(comment -> {
			comment.setUser(userService.getUserSimpleInfo(comment.getUserId()));
		});
		return new DataListUtil().data(thePage);
	}

	//查询一个实体的评论列表,里面还有楼中楼的第一页数据
	@Override
	public Map<String, Object> selectListById(int page, String id, int order, String userId) {
		thePage = new Page<>(page, 5);
		if (order == 0) {
			commentMapper.selectPage(thePage, new QueryWrapper<Comment>().eq("comment_id", id).orderByDesc("time"));
		}
		commentMapper.selectPage(thePage, new QueryWrapper<Comment>().eq("comment_id", id).orderByAsc("time"));
		//遍历数据进行再查询
		thePage.getRecords().forEach(comment -> {
			Page<Comment> p = new Page<>(1, 5);
			commentMapper.selectPage(p, new QueryWrapper<Comment>().eq("comment_id", comment.getId()).orderByAsc("time"));
			p.getRecords().forEach(comment1 -> {
				comment1.setUser(userService.getUserSimpleInfo(comment1.getUserId()));
			});
			comment.setCommentList(new DataListUtil().data(p));
			comment.setUser(userService.getUserSimpleInfo(comment.getUserId()));
			comment.setLikeNum(likeMapper.getEntityLikeNum(comment.getId()));
			if (userId != null) {
				comment.setIsLike(likeMapper.selectOne(new QueryWrapper<Like>().eq("like_id", userId).eq("entity_id", comment.getId())) != null ? 1 : 0);
			}
		});
		return new DataListUtil().data(thePage);
	}

	@Override
	public boolean addComment(Comment comment) {
		if (commentMapper.insert(comment) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public Comment selectById(String id) {
		Comment comment = commentMapper.selectById(id);
		comment.setUser(userService.getUserSimpleInfo(comment.getUserId()));
		return comment;
	}

	@Override
	public Map<String, Object> selectListAll() {
		thePage = new Page<>(1, 10);
		commentMapper.selectPage(thePage, new QueryWrapper<Comment>().orderByDesc("time"));
		thePage.getRecords().forEach(comment -> {
			comment.setUser(userService.getUserSimpleInfo(comment.getUserId()));
		});
		return new DataListUtil().data(thePage);
	}

	@Override
	public boolean delete(String id) {
		Comment comment = commentMapper.selectById(id);
		if (comment.getType().equals("dynamic") || comment.getType().equals("article")) {
			List<Comment> commentList = commentMapper.selectList(new QueryWrapper<Comment>().eq("comment_id", id));
			List<String> commentId = new ArrayList<>();
			commentList.forEach(comment_1 -> {
				commentId.add(comment_1.getId());
				likeService.delete(comment_1.getId());
			});
			commentMapper.delete(new QueryWrapper<Comment>().in("id", commentId));
		}
		commentMapper.deleteById(id);
		likeService.delete(id);
		return true;
	}
}
